
%{
#include <stdio.h>
#include <stdlib.h>
int charc = 0, wordc = 0, linec = 0;
int fcounter, fcounter_max;
char **filenames;

int yywrap();
%}

NL \n
WS [ \t]+

%%

{WS} { charc += yyleng;
       wordc ++; }
{NL} { charc += yyleng;
       wordc ++;
       linec ++; }
.    { charc ++; }

%%

int main(int argc, char **argv) {
  fcounter_max = argc;
  fcounter = 1;
  filenames = argv;
  if(argc <= 1) {
    printf("Usage: %s <file1> <file2> ...\n", argv[0]);
    exit(0);
  }
  yyin = (FILE *)fopen(filenames[1], "r");
  yylex();
  printf("%d %d %d\n", charc, wordc, linec);
}

int yywrap() {
  fclose(yyin);
  fcounter ++;
  if(fcounter < fcounter_max) {
    yyin = (FILE *)fopen(filenames[fcounter], "r");
    return 0; // More characters to scan
  } else 
    return 1; // End of all files
}
